package com.cbtx.module_sp.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

import java.lang.reflect.Method;

/**
 * author : joker.peng
 * e-mail : joker@fiture.com
 * date   : 2021/6/10 5:40 PM
 * desc   :
 */
public class SimUtil {

    /**
     * 反射获取 getSubscriberId ，既imsi
     *
     * @param subId
     * @return
     */
    public static String getSubscriberId(Context context, int subId) {
        TelephonyManager telephonyManager = (TelephonyManager) context
                .getSystemService(Context.TELEPHONY_SERVICE); // 取得相关系统服务
        Class<?> telephonyManagerClass = null;
        String imsi = null;
        try {
            telephonyManagerClass = Class.forName("android.telephony.TelephonyManager");

            if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.LOLLIPOP) {
                Method method = telephonyManagerClass.getMethod("getSubscriberId", int.class);
                imsi = (String) method.invoke(telephonyManager, subId);
            } else if (android.os.Build.VERSION.SDK_INT == android.os.Build.VERSION_CODES.LOLLIPOP) {
                Method method = telephonyManagerClass.getMethod("getSubscriberId", long.class);
                imsi = (String) method.invoke(telephonyManager, (long) subId);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return imsi;
    }

    /**
     * 反射获取 getSubscriptionId ，既 subid
     *
     * @param slotId 卡槽位置（0，1）
     * @return
     */
    public static int getSubscriptionId(Context context, int slotId) {
        try {
            Method datamethod;
            int setsubid = -1; //定义要设置为默认数据网络的subid
            //获取默认数据网络subid   getDefaultDataSubId
            Class<?> SubscriptionManager = Class.forName("android.telephony.SubscriptionManager");
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) { // >= 24  7.0
                datamethod = SubscriptionManager.getDeclaredMethod("getDefaultDataSubscriptionId");
            } else {
                datamethod = SubscriptionManager.getDeclaredMethod("getDefaultDataSubId");
            }
            datamethod.setAccessible(true);
            int SubId = (int) datamethod.invoke(SubscriptionManager);


            Method subManagermethod = SubscriptionManager.getDeclaredMethod("from", Context.class);
            subManagermethod.setAccessible(true);
            Object subManager = subManagermethod.invoke(SubscriptionManager, context);

            //getActiveSubscriptionInfoForSimSlotIndex  //获取卡槽0或者卡槽1  可用的subid
            Method getActivemethod = SubscriptionManager.getDeclaredMethod(
                    "getActiveSubscriptionInfoForSimSlotIndex", int.class);
            getActivemethod.setAccessible(true);
            Object msubInfo = getActivemethod.invoke(subManager, slotId);  //getSubscriptionId

            Class<?> SubInfo = Class.forName("android.telephony.SubscriptionInfo");

            //slot0   获取卡槽0的subid
            int subid = -1;
            if (msubInfo != null) {
                Method getSubId0 = SubInfo.getMethod("getSubscriptionId");
                getSubId0.setAccessible(true);
                subid = (int) getSubId0.invoke(msubInfo);
            }
            return subid;
        } catch (Exception e) {
        }
        return -1;
    }

    /**
     * 获取运营商 IMSI
     * 默认为 IMEI1对应的 IMSI
     *
     * @return
     */
    public static String getSimOperator(Context context) {
        TelephonyManager telephonyManager = (TelephonyManager) context
                .getSystemService(Context.TELEPHONY_SERVICE); // 取得相关系统服务
        return telephonyManager.getSimOperator();
    }

    /**
     * 根据卡槽位置 获取运营商 IMSI
     *
     * @param slotId 卡槽位置（0，1）
     * @return
     */
    public static String getSimOperator(Context context, int slotId) {
        int subid = getSubscriptionId(context, slotId);
        if (subid == -1) {
            return "";
        }

        String imsi = getSubscriberId(context, subid);
        if (!TextUtils.isEmpty(imsi)) {
            return imsi;
        }

        return "";
    }

    /**
     * 通过卡槽位置拿 IMEI
     *
     * @param slotId (0, 1卡槽位置）
     * @return
     */
    @SuppressLint("MissingPermission")
    public static String getImei(Context context, int slotId) {
        if (slotId != 0 && slotId != 1) {
            return null;
        }

        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
            return tm.getDeviceId(slotId);

        } else if (slotId == 0) {
            return tm.getDeviceId();

        } else {
            TelephonyManager telephonyManager = (TelephonyManager) context
                    .getSystemService(Context.TELEPHONY_SERVICE); // 取得相关系统服务
            Class<?> telephonyManagerClass = null;
            String imei = null;

            try {
                telephonyManagerClass = Class.forName("android.telephony.TelephonyManager");
                Method method = telephonyManagerClass.getMethod("getImei", int.class);
                imei = (String) method.invoke(telephonyManager, slotId);
            } catch (Exception e) {
                e.printStackTrace();
            }

            return imei;
        }
    }
}
